<?php

namespace Interno\UserBundle\EventListener;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubcriberInterface;
use Doctrine\ORM\EntityManager;

class UserCreationListener implements EventSubcriberInterface
{
    protected $em;
    protected $user;
    
    public function __construct(EntityManager $em)
    {
        $this->em = $em;        
    }
    
    public static function getSubcribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess'
        );
    }
    
    public function onRegistrationSuccess(FormEvent $event)
    {
        $this->user = $event->getForm()->getData();
        $group_name = 'admin';
        $entity = $this->em->getRepository('InternoUserBundle:Group')->findOneByName($group_name);
        $this->user->addGroup($entity);
        $this->em->flush();
    }
}